//
//  InformationViewController.m
//  MRBang
//
//  Created by 铅笔 on 16/5/11.
//  Copyright © 2016年 杭州易网新科技. All rights reserved.
//

#import "InformationViewController.h"
#import "InformationDetailVC.h"

@interface InformationViewController ()<UIWebViewDelegate>

@property (strong, nonatomic) UIWebView *browseWeb;

@end

@implementation InformationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //状态栏背景色
    UIView *statusBarView=[[UIView alloc] initWithFrame:CGRectMake(0, 0,kScreenWidth, 20)];
    [self.view addSubview:statusBarView];
    
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:NO];
    
    self.navigationItem.leftBarButtonItem = nil;
    
    self.title = @"资讯";
    self.view.backgroundColor = COLOR_EVERYVIEWBG;
    
    self.browseWeb = [[UIWebView alloc] initWithFrame:CGRectMake(0, 64, kScreenWidth, kScreenHeight-64-49)];
    [self.view addSubview:self.browseWeb];
    
    NSURL *url=[NSURL URLWithString:@"http://www.mrbang.com.cn/news.php"];
    [self.browseWeb loadRequest:[NSURLRequest requestWithURL:url]];
    //此处是处理网页黑条条的状况
    self.browseWeb.opaque = NO;
    self.browseWeb.backgroundColor = [UIColor clearColor];
    [self.browseWeb setScalesPageToFit:NO];
    self.browseWeb.delegate = self;
    
    //下拉刷新
    [self PullSownRefresh];
}

/**
 *  下拉刷新
 */
- (void) PullSownRefresh
{
    __unsafe_unretained UIWebView *webView = self.browseWeb;
    webView.delegate = self;
    
    __unsafe_unretained UIScrollView *scrollView = self.browseWeb.scrollView;
    
    // 添加下拉刷新控件
    scrollView.header= [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [webView reload];
    
    }];
    
    // 如果是上拉刷新，就以此类推
}

#pragma mark 设置UIWebView
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    NSURL *url = [request URL];
    NSLog(@"url为%@",url);
    
    BOOL returnValue = YES;
    
    if([[url absoluteString] containsString:@"http://www.mrbang.com.cn/newsinfo.php"])
    {
        
        InformationDetailVC *detailView = [[InformationDetailVC alloc] init];
        detailView.str_url = [url absoluteString];
        [self.navigationController pushViewController:detailView animated:YES];
        
        returnValue =  NO;
    }
        return returnValue;
}

- (void)webViewDidStartLoad:(UIWebView *)webView
{
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
    [self.browseWeb.scrollView.header endRefreshing];
}

- (void) viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
    [self.browseWeb.scrollView.header endRefreshing];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}


@end
